package com.ruoyi.web.controller.jl;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.base.Result;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.support.Convert;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.base.BaseController;
import com.ruoyi.jl.domain.Game;
import com.ruoyi.jl.service.IGameService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 游戏 控制层
 * 
 * @author wh
 * @date 2019-04-29
 */
@Slf4j
@Controller
@RequestMapping("/jl/game")
public class GameController extends BaseController {
    private String prefix = "jl/game";
	
	private final IGameService gameService;

	@Autowired
	public GameController(IGameService gameService) {
		this.gameService = gameService;
	}

	/**
	*  列表页
	*/
	@RequiresPermissions("jl:game:view")
	@GetMapping
	public String game(){
	    return prefix + "/game";
	}

	/**
	 * 新增页
	 */
	@GetMapping("/add")
	public String add(){
		return prefix + "/add";
	}

	/**
	 * 编辑页
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") Integer id, ModelMap mmap){
		Game game = gameService.getById(id);
		mmap.put("game", game);
		return prefix + "/edit";
	}
	
	/**
	 * 查询游戏列表
	 */
	@RequiresPermissions("jl:game:list")
	@PostMapping("/list")
	@ResponseBody
	public Result list(Game game){
		startPage();
        List<Game> list = gameService.list(game);
		return Result.success(getDataTable(list));
	}

	/**
	 * 新增保存游戏
	 */
	@RequiresPermissions("jl:game:add")
	@Log(title = "游戏", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public Result addSave(Game game){
		game.setStatus(0);
		gameService.save(game);
		return Result.success();
	}

	/**
	 * 修改保存游戏
	 */
	@RequiresPermissions("jl:game:edit")
	@Log(title = "游戏", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public Result editSave(Game game){
		gameService.updateById(game);
		return Result.success();
	}
	
	/**
	 * 删除游戏
	 */
	@RequiresPermissions("jl:game:remove")
	@Log(title = "游戏", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public Result remove(String ids){
		Long[] longIds = Convert.toLongArray(ids);
		// gameService.removeByIds(Arrays.asList(longIds));
		for(Long id : longIds) {
			Game game = gameService.getBaseMapper().selectById(id);
			game.setStatus(1);
			gameService.updateById(game);
		}
		return Result.success();
	}

	/**
	 * 导出游戏列表
	 */
	@RequiresPermissions("jl:game:export")
	@PostMapping("/export")
	@ResponseBody
	public Result export(Game game){
		List<Game> list = gameService.list(game);
		ExcelUtil<Game> util = new ExcelUtil<>(Game.class);
		util.exportExcel(list, "game");
		return Result.success();
	}

	/**
	 * 选择部门树页面
	 */
	@GetMapping("/selectGameTree/{gameId}")
	public String selectGameTree(@PathVariable("gameId") Long gameId, ModelMap mmap) {
		Game game = gameService.selectDefaultTree(gameId);
		mmap.put("game", game);
		return prefix + "/tree";
	}

	/**
	 * 加载部门列表树
	 */
	@GetMapping("/treeData")
	@ResponseBody
	public List<Map<String, Object>> treeData() {
		List<Map<String, Object>> trees = gameService.treeData();
		return trees;
	}
}
